package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private LoginService loginService;

    @RequestMapping("/check")
    public Result check(@RequestBody Map map,HttpServletResponse response){
        try {
            /*判断验证码是否正确*/
            String telephone = (String) map.get("telephone");
            String validateCode = (String) map.get("validateCode");//获取前台验证码
            String validateCode1 = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);//获取后台验证码;
            if(validateCode!=null && validateCode1!=null && validateCode1.equals(validateCode)){
    //          验证码输入正确;查询该用户是否为会员
                Member memberByPhone = loginService.findMemberByPhone(telephone);
                if(memberByPhone==null){
    //                用户不是会员,注册为会员，并登录
                    Member member = new Member();
                    member.setRegTime(new Date());
                    member.setPhoneNumber(telephone);
                    loginService.addMember(member);
    //                会员注册成功，登录成功
                    login(telephone,member,response);
                    return new Result(true,MessageConstant.LOGIN_SUCCESS);
                }else {
                    //用户是会员登录成功
                    login(telephone,memberByPhone,response);
                    return new Result(true,MessageConstant.LOGIN_SUCCESS);
                }
            }else {
                //验证码错误
                return new Result(true,MessageConstant.VALIDATECODE_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"服务器异常，请稍后再试");
        }
    }

    //登录
    public void login(String telephone,Member member, HttpServletResponse response){
        //发送Cookie
        Cookie cookie = new Cookie("login_member_telephone", telephone+RedisMessageConstant.SENDTYPE_LOGIN);
        cookie.setMaxAge(60*60*24*30);
        cookie.setPath("/");
        response.addCookie(cookie);
        //将用户信息存入Redis
        String json = JSON.toJSON(member).toString();
        jedisPool.getResource().set(telephone+RedisMessageConstant.SENDTYPE_LOGIN,json);

    }




}
